﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using System.Text;

namespace WebUI.LabManage
{
    public partial class AbandonRecord : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        protected void bnSearch_Click(object sender, EventArgs e)
        {
            if (tb_cause.Text == "" && tb_checker.Text == "" && dp_dateUP.SelectedDate == null && dp_dateDOWN.SelectedDate == null)
            {
                ExtAspNet.PageContext.RegisterStartupScript(Alert.GetShowInTopReference("你没有输入任何查询条件", "提示！", MessageBoxIcon.Warning));
            }
            else
            {
                if (dp_dateUP.SelectedDate > dp_dateDOWN.SelectedDate)
                {
                    ExtAspNet.PageContext.RegisterStartupScript(Alert.GetShowInTopReference("两个日期不能组成范围！", "提示！", MessageBoxIcon.Warning));
                }
                else
                {
                    StringBuilder strWhere = new StringBuilder();
                    if (tb_checker.Text != "")
                    {
                        strWhere.AppendFormat(" checker LIKE '%{0}%'", tb_checker.Text);
                    }
                    if (tb_cause.Text != "")
                    {
                        if (tb_checker.Text != "")
                        {
                            strWhere.Append(" AND");
                        }
                        strWhere.AppendFormat(" cause LIKE '%{0}%'", tb_cause.Text);
                    }
                    if (dp_dateUP.SelectedDate != null)
                    {
                        if (tb_checker.Text != "" || tb_cause.Text != "")
                        {
                            strWhere.Append(" AND");
                        }
                        strWhere.AppendFormat(" date >= '{0}'",dp_dateUP.SelectedDate);
                    }
                    if(dp_dateDOWN.SelectedDate != null)
                    {
                        if (tb_checker.Text != "" || tb_cause.Text != "" || dp_dateUP.SelectedDate != null)
                        {
                            strWhere.Append(" AND");
                        }
                        strWhere.AppendFormat(" date <= '{0}'", dp_dateDOWN.SelectedDate);
                    }
                    Grid1.DataSource = BLL.EquipmentRecord.Abandon.GetList(strWhere.ToString());
                    Grid1.DataBind();
                }
            }
        }
        protected void bnReset_Click(object sender, EventArgs e)
        {
            InitForm(); 
            BindGrid();

        }
        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
        }

        private void BindGrid()
        {
            Grid1.DataSource = BLL.EquipmentRecord.Abandon.GetList();
            Grid1.DataBind();
        }

        private void InitForm()
        {
            tb_cause.Text = "";
            tb_checker.Text = "";
            dp_dateUP.SelectedDate = null;
            dp_dateDOWN.SelectedDate = null;
        }
    }
}